<?php
/*
	 *	  match_priv_level() function to decide if the user is in privilage level
	      given or not; Each privilage level is given in const-number available in
		  configuration file [configure.php].
     *    Privilage constants have the prefix P- ex P-ADMIN
     *    Privilage checker - session based
     *    @return Boolean
	 *	  @param  Integer $level : privilage level ex: 1 admin, 2 user 3 guest etc ..
*/
function match_priv_level($level){
	  if($_SESSION['priv'] == $level){
			return true;
	  }
	  return false;
}
/*
     *    Load Suitable file containg the suitable process function declaration
     *    if requested function isn't available it recurses to ssol
     *@return void
*/
function choose_ctrl(&$req,$ctrlr){
	  //print_r($ctrlr);
	  $do=$req['do'];
	  $shrtCtrlrs=array_keys($ctrlr);
	  if (in_array($req['do'],$shrtCtrlrs)){
			require("{$ctrlr[$do]}.php");
			return process($req);
	  }
}
/*
     *    Java script redirection
     *    redirects while leaving message
     *    redirection delay 1.5 seconds
     *    @return Void
     *    @param  msg   default value: Please, wait while redirecting ..
*/
function Jredir($path,$msg="Please, wait while redirecting .."){
	 $path=array('path'=>$path,
				  'msg'=>$msg);
     $html.= render('error',$path);
	 return $html;
}
/*
     *    Request validation
     *    Empty the $_GET and $_POST arrays and the equivilant keys in the $_REQUEST array keys
     *    Strip HTML tags from request
     *    HTML entities
     *    Add slashes
     *    @return $req validated request array with same request key names
*/
function validate_request(){
	  validate_post($req);
	  validate_get($req);
	  return validate_html($req);
}
/*
     *     template renderer
     *     render given file with the given array.
     *    @param $file : template file name without .html
     *    @param $dataArray : array of keys => data to be used in rendering process.
     *    @example {key} will be replaced with $dataArray['key'].
     *    @return HTML rendered
*/
function render($file,$dataArray,$theme=THEME){
     $rawHtml=easy_render($file,$theme);
     $html=render_var($rawHtml,$dataArray);
     return $html;
}
/*
     *    no render
     *    get the content of template file "raw html without any processing"
     *    @param $file : template file name without .html
     *    @return Raw HTML Text
*/
function easy_render($file,$theme=THEME){
     return file_get_contents("template/{$theme}/{$file}.html");
}
/*
     *    String renderer
     *    render given string variable with the given array
     *    @param $rawHtml : String containg "commonly" HTML
     *    @param $dataArray : array of keys => data to be used in rendering process.
     *    @example {key} will be replaced with $dataArray['key']
     *    @return HTML rendered
*/
function render_var($rawHtml,$dataAr){
     $keys=array_keys($dataAr);
     foreach ($keys as &$v){
          $v="{".$v."}";
     }
     $values=array_values($dataAr);
     $html=str_replace($keys,$values,$rawHtml);
     return $html;
}
/*
     *    header template loader
     *    render the header template based on user privilage
     *    @return HTML rendered
*/
function var_header(){
     //load variable header based on user session ..
     
}
/*
     *    footer template loader
     *    render the footer template based on user privilage
     *    @return HTML rendered
*/
function var_footer(){
     //load variable footer based on user session ..
}
function validateEmail($email){
$pattern = "#^([_a-z0-9-]+)(\.[a-z0-9]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,5})$#";
	  if(eregi($pattern,$email)){
			$NEW['email'] = $email;
			return $NEW["$email"];
       }else{
          die("Invalid E-mail");
       }
}
function validatePasswd($pass1,$pass2){
	  if($pass1 == "" || $pass2 == ""){
			die("Password field can't ba left blank");
       }elseif($pass1 !== $pass2){
          die("Paswords don't match");
       }else{
			$NEW['passwd'] = $pass1;
			return $NEW["passwd"];
       }
}
function validatName($name){
	  $pattern = "#^([_a-z0-9-\.]+)([a-z0-9_]+)$#";
	  if(preg_match($pattern,$name)){
			$NEW["$name"] = $name;
			return $NEW["$name"];
	  }else{
			die("Invalid name");
	  }
}
function query($query,$type=1,$success="success sending query",$failure="failed !!"){
      mysql_connect(DB_HOST,DB_USER,DB_PASS) or die ('Error connecting'.mysql_error()); // connected
      mysql_select_db(DB_NAME) or die ('Error selecting db'.mysql_error()); // selecting db
      if($type){
	mysql_query($query)  or die ($failure.mysql_error()); // sending query
	echo $success;
	return;
      }else{
	$result=mysql_query($query)  or die ($failure.mysql_error()); // sending query
	echo $success;
	return $result;
      }
}
function validate_post(&$req){
	  $req= array();
	  foreach($_POST as $k=>$v){
			$req[$k]=$v;
			unset($_REQUEST[$k]);
	  }
	  unset($_POST);
	  return $req;
}
function validate_get(&$req){
	  $req;
	  foreach($_GET as $k=>$v){
			$req[$k]=$v;
			unset($_REQUEST[$k]);
	  }
	  unset($_GET);
	  return $req;
}
function validate_html(&$req){
	  if ( isset( $req ) ){
			foreach($req as $k=>$v){
				  if(is_string($v)){
						$allowedTags='<a><br><b><h1><h2><h3><h4><i>' .
						'<img><li><ol><p><strong><table>' .
						'<tr><td><th><u><ul>';
						strip_tags($v,$allowedTags);
						htmlentities($v);
						addslashes($v);
				  }
			}
	  }
	  return $req;
}
?>
